const order = require('../../services/order.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        info: '',
        address: null,
        payType: '',
        login: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id,
            login: wx.getStorageSync("login") === '' ? false : wx.getStorageSync("login"),
        })
        this.getInfo()
        this.getAddress()
    },

    getAddress(){
        order.address({},(res)=>{
            if(res.code===0){
                this.setData({
                    address:JSON.parse(res.msg)
                })
            }
        })
    },

    getInfo() {
        const data = {
            id: this.data.id
        }
        order.get(data, (res) => {
            if (res.code === 0) {
                console.log(res.data.aid)
                if(res.data.aid!=null){
                    console.log(res.data.aid)
                    this.setData({
                        info: res.data,
                        address: JSON.parse(res.data.aid),
                        payType: res.data.payType == null ? '1' : res.data.payType,
                    })
                }else{
                    this.setData({
                        info: res.data,
                        payType: res.data.payType == null ? '1' : res.data.payType,
                    })
                }
            }
        })
    },

    toPay() {
        if (this.data.address === '' || this.data.address === null) {
            wx.showModal({
                title: '请选择收货地址',
                showCancel: false
            })
            return;
        }
        wx.showLoading({
            title: '支付中'
        })
        let data = this.data.address
        data['id'] = this.data.id
        data['payType'] = this.data.payType
        order.pay(data, (res) => {
            if (res.code === 0) {
                wx.redirectTo({
                    url: '../success/success?id=' + res.msg
                })
            } else {
                wx.showToast({
                    title: '失败',
                    icon: 'error',
                    duration: 2000
                })
            }
            wx.hideLoading()
        })
    },

    chooseAddress() {
        const that = this
        wx.chooseAddress({
            success(res) {
                console.log(res)
                let info = that.data.info
                info['aid'] = res
                that.setData({
                    address: res,
                    info: info
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
